/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.DAO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.RollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import vn.com.sml.SEFM.domain.Cities;
import vn.com.sml.SEFM.domain.Countries;
import vn.com.sml.SEFM.util.HibernateUtil;
/**
 *
 * @author Tunp
 */
public class CitiesDAO {
    
    public static List<Cities> listCities() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Cities> lst = new ArrayList<Cities>();
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstCities = session.createQuery("from Cities").list();
            for (Iterator iterator = (Iterator) lstCities.iterator();iterator.hasNext();)
            {
                Cities cities = (Cities) iterator.next();
                Countries ctr = (Countries) session.createQuery("select i.countries from Cities as i where i.cityId="+cities.getCityId()).uniqueResult();
                cities.setCountries(ctr);
                lst.add(cities);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }
    
    public static Cities getCitiesById(int id)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Cities cities = null;
        
        try {
            transaction = (Transaction) session.beginTransaction();
            
            cities = (Cities) session.createQuery("from Cities where cityId="+id).uniqueResult();
            
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return cities;
    }
    
    public static List<Countries> listCountries() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Countries> lst = new ArrayList<Countries>();
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstCountries = session.createQuery("from Countries").list();
            for (Iterator iterator = (Iterator) lstCountries.iterator();iterator.hasNext();)
            {
                Countries country = (Countries) iterator.next();
                
                lst.add(country);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }

    public static List<Cities> getListCitiesByCountry(int country_id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Cities> lst = new ArrayList<Cities>();
        
        try {
            transaction = (Transaction) session.beginTransaction();
            List lstCities = session.createQuery("from Cities").list();
            lstCities = session.createQuery("from Cities where countriesId='"+country_id+"'").list();
            for (Iterator iterator = (Iterator) lstCities.iterator();iterator.hasNext();)
            {
                Cities Cities = (Cities) iterator.next();
                
                lst.add(Cities);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    
    }
}

